package com.inei.cenacom.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlSerializer;

import com.inei.cenacom.model.entity.Cap100;
import com.inei.cenacom.model.entity.Cap200;
import com.inei.cenacom.model.entity.Cap300;
import com.inei.cenacom.model.entity.Cap400;
import com.inei.cenacom.model.entity.Caratula;
import com.inei.cenacom.model.entity.Entrevista;

import android.content.Context;
import android.os.Environment;
import android.util.Xml;

public class Exportar {

	private Context context;

	public Exportar(Context context) {
		this.context = context;
	}

	public boolean xml(Caratula caratula, List<Entrevista> lEntrevista,
			Cap100 cap100, List<Cap200> lCap200, List<Cap300> lCap300,
			List<Cap400> lCap400) {
		File file = null;
		File ruta = null;
		try {
			File root = Environment.getExternalStorageDirectory();
			ruta = new File(root, "/CENACOM/");
			if (!ruta.exists()) {
				ruta.mkdirs();
			}
			SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyHHmmss");
			String date = sdf.format(Calendar.getInstance().getTime());
			file = new File(ruta, "CENACOM_" + caratula.getID_N() + "_"
					+ caratula.getANIO()+"_"+date + ".xml");
			FileOutputStream fos = new FileOutputStream(file);
			XmlSerializer xml = Xml.newSerializer();
			xml.setOutput(fos, HTTP.UTF_8);
			xml.startDocument(HTTP.UTF_8, true);
			xml.startTag("", "script");

			// CARATULA
			xml.startTag("", "CARATULA");

			xml.startTag("", "ID_N");
			xml.text(Texto(caratula.getID_N()));
			xml.endTag("", "ID_N");

			xml.startTag("", "ANIO");
			xml.text(""+caratula.getANIO());
			xml.endTag("", "ANIO");
			
			xml.startTag("", "DPTO_ASIG");
			xml.text(""+caratula.getDPTO_ASIG());
			xml.endTag("", "DPTO_ASIG");
			
			xml.startTag("", "CCDD");
			xml.text(Texto(caratula.getCCDD()));
			xml.endTag("", "CCDD");

			xml.startTag("", "NOMBREDD");
			xml.text(Texto(caratula.getNOMBREDD()));
			xml.endTag("", "NOMBREDD");

			xml.startTag("", "CCPP");
			xml.text(Texto(caratula.getCCPP()));
			xml.endTag("", "CCPP");

			xml.startTag("", "NOMBREPP");
			xml.text(Texto(caratula.getNOMBREPP()));
			xml.endTag("", "NOMBREPP");

			xml.startTag("", "CCDI");
			xml.text(Texto(caratula.getCCDI()));
			xml.endTag("", "CCDI");

			xml.startTag("", "NOMBREDI");
			xml.text(Texto(caratula.getNOMBREDI()));
			xml.endTag("", "NOMBREDI");

			xml.startTag("", "CCCP");
			xml.text(Texto(caratula.getNOMBRE_CP()));
			xml.endTag("", "CCCP");

			xml.startTag("", "ZONA");
			xml.text(Texto(caratula.getZONA()));
			xml.endTag("", "ZONA");

			xml.startTag("", "ZONAALF");
			xml.text(Texto(caratula.getZONAALF()));
			xml.endTag("", "ZONAALF");

			xml.startTag("", "MZA");
			xml.text(Texto(caratula.getMZA()));
			xml.endTag("", "MZA");

			xml.startTag("", "MZNALF");
			xml.text(Texto(caratula.getMZAALF()));
			xml.endTag("", "MZNALF");

			xml.startTag("", "AER_INI");
			xml.text(Texto(caratula.getAER_INI()));
			xml.endTag("", "AER_INI");

			xml.startTag("", "AER_FIN");
			xml.text(Texto(caratula.getAER_FIN()));
			xml.endTag("", "AER_FIN");

			xml.startTag("", "I9_1");
			xml.text("" + caratula.getI9_1());
			xml.endTag("", "I9_1");

			xml.startTag("", "I9_2");
			xml.text(Texto(caratula.getI9_2()));
			xml.endTag("", "I9_2");

			xml.startTag("", "I9_4");
			xml.text(Texto(caratula.getI9_4()));
			xml.endTag("", "I9_4");

			xml.startTag("", "I9_5");
			xml.text(Texto(caratula.getI9_5()));
			xml.endTag("", "I9_5");

			xml.startTag("", "I9_6");
			xml.text(Texto(caratula.getI9_6()));
			xml.endTag("", "I9_6");

			xml.startTag("", "I9_7");
			xml.text(Texto(caratula.getI9_7()));
			xml.endTag("", "I9_7");

			xml.startTag("", "I9_8");
			xml.text(Texto(caratula.getI9_8()));
			xml.endTag("", "I9_8");

			xml.startTag("", "I9_9");
			xml.text("" + caratula.getI9_9());
			xml.endTag("", "I9_9");

			xml.startTag("", "I9_10");
			xml.text(Texto(caratula.getI9_10()));
			xml.endTag("", "I9_10");

			xml.startTag("", "I9_3");
			xml.text(Texto(caratula.getI9_3()));
			xml.endTag("", "I9_3");

			xml.startTag("", "II1");
			xml.text(Texto(caratula.getII1()));
			xml.endTag("", "II1");

			xml.startTag("", "II2");
			xml.text(Texto(caratula.getII2()));
			xml.endTag("", "II2");

			xml.startTag("", "II3");
			xml.text(Texto(caratula.getII3()));
			xml.endTag("", "II3");

			xml.startTag("", "II4");
			xml.text("" + caratula.getII4());
			xml.endTag("", "II4");

			xml.startTag("", "II5");
			xml.text("" + caratula.getII5());
			xml.endTag("", "II5");

			xml.startTag("", "II6");
			xml.text(Texto(caratula.getII6()));
			xml.endTag("", "II6");

			xml.startTag("", "II7");
			xml.text("" + caratula.getII7());
			xml.endTag("", "II7");

			xml.startTag("", "II8");
			xml.text("" + caratula.getII8());
			xml.endTag("", "II8");

			xml.startTag("", "II8_O");
			xml.text(Texto(caratula.getII8_O()));
			xml.endTag("", "II8_O");

			xml.startTag("", "II9");
			xml.text("" + caratula.getII9());
			xml.endTag("", "II9");

			xml.startTag("", "II9_O");
			xml.text(Texto(caratula.getII9_O()));
			xml.endTag("", "II9_O");

			xml.startTag("", "IV2_1_D");
			xml.text(Texto(caratula.getIV2_1_D()));
			xml.endTag("", "IV2_1_D");

			xml.startTag("", "IV2_1_M");
			xml.text(Texto(caratula.getIV2_1_M()));
			xml.endTag("", "IV2_1_M");

			xml.startTag("", "IVRESFIN_02");
			xml.text("" + caratula.getIVRESFIN_02());
			xml.endTag("", "IVRESFIN_02");

			xml.startTag("", "IVRESFIN_02_O");
			xml.text(Texto(caratula.getIVRESFIN_02_O()));
			xml.endTag("", "IVRESFIN_02_O");

			xml.startTag("", "V_1");
			xml.text("" + caratula.getNRO_DELITOS());
			xml.endTag("", "V_1");

			xml.startTag("", "V_2");
			xml.text("" + caratula.getNRO_DENUNCIAS());
			xml.endTag("", "V_2");

			xml.startTag("", "V_3");
			xml.text("" + caratula.getNRO_VICTIMAS_FALLECIDAS());
			xml.endTag("", "V_3");

			xml.startTag("", "V_4");
			xml.text("" + caratula.getNRO_VICTIMAS_HOMICIDIO());
			xml.endTag("", "V_4");

			xml.startTag("", "VI1A");
			xml.text(Texto(caratula.getVI1A()));
			xml.endTag("", "VI1A");

			xml.startTag("", "VI1B");
			xml.text(Texto(caratula.getVI1B()));
			xml.endTag("", "VI1B");

			xml.startTag("", "VI2A");
			xml.text(Texto(caratula.getVI2A()));
			xml.endTag("", "VI2A");

			xml.startTag("", "VI2B");
			xml.text(Texto(caratula.getVI2B()));
			xml.endTag("", "VI2B");

			xml.startTag("", "VI3A");
			xml.text(Texto(caratula.getVI3A()));
			xml.endTag("", "VI3A");

			xml.startTag("", "VI3B");
			xml.text(Texto(caratula.getVI3B()));
			xml.endTag("", "VI3B");

			xml.startTag("", "VII1A");
			xml.text(Texto(caratula.getVII1A()));
			xml.endTag("", "VII1A");

			xml.startTag("", "VII1B");
			xml.text(Texto(caratula.getVII1B()));
			xml.endTag("", "VII1B");

			xml.startTag("", "VII1C");
			xml.text(Texto(caratula.getVII1C()));
			xml.endTag("", "VII1C");

			xml.startTag("", "VII2A");
			xml.text(Texto(caratula.getVII2A()));
			xml.endTag("", "VII2A");

			xml.startTag("", "VII2B");
			xml.text(Texto(caratula.getVII2B()));
			xml.endTag("", "VII2B");

			xml.startTag("", "VII2C");
			xml.text(Texto(caratula.getVII2C()));
			xml.endTag("", "VII2C");

			xml.startTag("", "VII1D");
			xml.text(Texto(caratula.getVII1D()));
			xml.endTag("", "VII1D");

			xml.startTag("", "VII1E");
			xml.text(Texto(caratula.getVII1E()));
			xml.endTag("", "VII1E");

			xml.startTag("", "VII1F");
			xml.text(Texto(caratula.getVII1F()));
			xml.endTag("", "VII1F");

			xml.startTag("", "VII2D");
			xml.text(Texto(caratula.getVII2D()));
			xml.endTag("", "VII2D");

			xml.startTag("", "VII2E");
			xml.text(Texto(caratula.getVII2E()));
			xml.endTag("", "VII2E");

			xml.startTag("", "VII2F");
			xml.text(Texto(caratula.getVII2F()));
			xml.endTag("", "VII2F");

			xml.startTag("", "OBS_CAR");
			xml.text(Texto(caratula.getOBS_CAR()));
			xml.endTag("", "OBS_CAR");
			
			xml.startTag("", "USR_CREACION");
			xml.text(Texto(caratula.getUSR_CREACION()));
			xml.endTag("", "USR_CREACION");
			
			xml.startTag("", "FEC_CREACION");
			xml.text(Texto(caratula.getFEC_CREACION()));
			xml.endTag("", "FEC_CREACION");
			
			xml.startTag("", "USR_EDICION");
			xml.text(Texto(caratula.getUSR_EDICION()));
			xml.endTag("", "USR_EDICION");
			
			xml.startTag("", "FEC_EDICION");
			xml.text(Texto(caratula.getFEC_EDICION()));
			xml.endTag("", "FEC_EDICION");

			xml.endTag("", "CARATULA");

			// ENTREVISTAS
			if (lEntrevista != null) {
				//for (Entrevista entrevista : lEntrevista) {
				for(int i = 0 ; i<lEntrevista.size();i++){
					Entrevista entrevista = lEntrevista.get(i);
					xml.startTag("", "CARATULA_ENTREVISTA");

					xml.startTag("", "ID_N");
					xml.text(Texto(entrevista.getID_N()));
					xml.endTag("", "ID_N");
					
					xml.startTag("", "ANIO");
					xml.text(Texto(entrevista.getANIO()));
					xml.endTag("", "ANIO");

					xml.startTag("", "III2_1");
					xml.text("" + (i+1));
					xml.endTag("", "III2_1");

					xml.startTag("", "III2_2_D");
					xml.text(Texto(entrevista.getIII2_2_D()));
					xml.endTag("", "III2_2_D");

					xml.startTag("", "III2_2_M");
					xml.text(Texto(entrevista.getIII2_2_M()));
					xml.endTag("", "III2_2_M");

					xml.startTag("", "III2_3A_IH");
					xml.text(Texto(entrevista.getIII2_3A_IH()));
					xml.endTag("", "III2_3A_IH");

					xml.startTag("", "III2_3A_IM");
					xml.text(Texto(entrevista.getIII2_3A_IM()));
					xml.endTag("", "III2_3A_IM");

					xml.startTag("", "III2_3B_FH");
					xml.text(Texto(entrevista.getIII2_3B_FH()));
					xml.endTag("", "III2_3B_FH");

					xml.startTag("", "III2_3B_FM");
					xml.text(Texto(entrevista.getIII2_3B_FM()));
					xml.endTag("", "III2_3B_FM");

					xml.startTag("", "III2_4A_D");
					xml.text(Texto(entrevista.getIII2_4A_D()));
					xml.endTag("", "III2_4A_D");

					xml.startTag("", "III2_4A_M");
					xml.text(Texto(entrevista.getIII2_4A_M()));
					xml.endTag("", "III2_4A_M");

					xml.startTag("", "III2_4B_H");
					xml.text(Texto(entrevista.getIII2_4B_H()));
					xml.endTag("", "III2_4B_H");

					xml.startTag("", "III2_4B_M");
					xml.text(Texto(entrevista.getIII2_4B_M()));
					xml.endTag("", "III2_4B_M");

					xml.startTag("", "III2_5");
					xml.text("" + entrevista.getIII2_5());
					xml.endTag("", "III2_5");

					xml.startTag("", "III2_6_D");
					xml.text(Texto(entrevista.getIII2_6_D()));
					xml.endTag("", "III2_6_D");

					xml.startTag("", "III2_6_M");
					xml.text(Texto(entrevista.getIII2_6_M()));
					xml.endTag("", "III2_6_M");

					xml.startTag("", "III2_7A_H");
					xml.text(Texto(entrevista.getIII2_7A_H()));
					xml.endTag("", "III2_7A_H");

					xml.startTag("", "III2_7A_M");
					xml.text(Texto(entrevista.getIII2_7A_M()));
					xml.endTag("", "III2_7A_M");

					xml.startTag("", "III2_7B_H");
					xml.text(Texto(entrevista.getIII2_7B_H()));
					xml.endTag("", "III2_7B_H");

					xml.startTag("", "III2_7B_M");
					xml.text(Texto(entrevista.getIII2_7B_M()));
					xml.endTag("", "III2_7B_M");

					xml.startTag("", "III2_8");
					xml.text("" + entrevista.getIII2_8());
					xml.endTag("", "III2_8");

					xml.startTag("", "GPSLATITUD_INI");
					xml.text(Texto( entrevista.getGPSLATITUD_I()));
					xml.endTag("", "GPSLATITUD_INI");

					xml.startTag("", "GPSLONGITUD_INI");
					xml.text(Texto( entrevista.getGPSLONGITUD_I()));
					xml.endTag("", "GPSLONGITUD_INI");

					xml.startTag("", "GPSLATITUD_FIN");
					xml.text(Texto( entrevista.getGPSLATITUD_F()));
					xml.endTag("", "GPSLATITUD_FIN");

					xml.startTag("", "GPSLONGITUD_FIN");
					xml.text(Texto( entrevista.getGPSLONGITUD_F()));
					xml.endTag("", "GPSLONGITUD_FIN");

					xml.startTag("", "USR_CREACION");
					xml.text(Texto(entrevista.getUSR_CREACION()));
					xml.endTag("", "USR_CREACION");
					
					xml.startTag("", "FEC_CREACION");
					xml.text(Texto(entrevista.getFEC_CREACION()));
					xml.endTag("", "FEC_CREACION");
					
					xml.startTag("", "USR_EDICION");
					xml.text(Texto(entrevista.getUSR_EDICION()));
					xml.endTag("", "USR_EDICION");
					
					xml.startTag("", "FEC_EDICION");
					xml.text(Texto(entrevista.getFEC_EDICION()));
					xml.endTag("", "FEC_EDICION");
					
					
					xml.endTag("", "CARATULA_ENTREVISTA");
				}
			}

			// CAP100
			if (cap100 != null) {
				xml.startTag("", "CAP100");

				xml.startTag("", "ID_N");
				xml.text(Texto(cap100.getID_N()));
				xml.endTag("", "ID_N");
				
				xml.startTag("", "ANIO");
				xml.text(Texto(cap100.getANIO()));
				xml.endTag("", "ANIO");
				
				xml.startTag("", "DN101");
				xml.text("" + cap100.getDN101());
				xml.endTag("", "DN101");

				xml.startTag("", "DN101_1");
				xml.text("" + cap100.getDN101_1());
				xml.endTag("", "DN101_1");

				xml.startTag("", "DN101_1_A");
				xml.text("" + cap100.getDN101_1_A());
				xml.endTag("", "DN101_1_A");

				xml.startTag("", "DN101_1_B");
				xml.text("" + cap100.getDN101_1_B());
				xml.endTag("", "DN101_1_B");

				xml.startTag("", "DN101_1_C");
				xml.text("" + cap100.getDN101_1_C());
				xml.endTag("", "DN101_1_C");

				xml.startTag("", "DN101_1_D");
				xml.text("" + cap100.getDN101_1_D());
				xml.endTag("", "DN101_1_D");

				xml.startTag("", "DN101_1_E");
				xml.text("" + cap100.getDN101_1_E());
				xml.endTag("", "DN101_1_E");

				xml.startTag("", "DN101_1_F");
				xml.text("" + cap100.getDN101_1_F());
				xml.endTag("", "DN101_1_F");

				xml.startTag("", "DN101_1_G");
				xml.text("" + cap100.getDN101_1_G());
				xml.endTag("", "DN101_1_G");

				xml.startTag("", "DN101_2");
				xml.text("" + cap100.getDN101_2());
				xml.endTag("", "DN101_2");

				xml.startTag("", "DN101_3");
				xml.text("" + cap100.getDN101_3());
				xml.endTag("", "DN101_3");

				xml.startTag("", "DN101_4");
				xml.text("" + cap100.getDN101_4());
				xml.endTag("", "DN101_4");

				xml.startTag("", "DN101_5");
				xml.text("" + cap100.getDN101_5());
				xml.endTag("", "DN101_5");

				xml.startTag("", "DN101_6");
				xml.text("" + cap100.getDN101_6());
				xml.endTag("", "DN101_6");

				xml.startTag("", "DN101_7");
				xml.text("" + cap100.getDN101_7());
				xml.endTag("", "DN101_7");

				xml.startTag("", "DN101_8");
				xml.text("" + cap100.getDN101_8());
				xml.endTag("", "DN101_8");

				xml.startTag("", "DN101_9");
				xml.text("" + cap100.getDN101_9());
				xml.endTag("", "DN101_9");

				xml.startTag("", "DN101_10");
				xml.text("" + cap100.getDN101_10());
				xml.endTag("", "DN101_10");

				xml.startTag("", "DN101_11");
				xml.text("" + cap100.getDN101_11());
				xml.endTag("", "DN101_11");

				xml.startTag("", "DN102");
				xml.text("" + cap100.getDN102());
				xml.endTag("", "DN102");

				xml.startTag("", "DN103");
				xml.text("" + cap100.getDN103());
				xml.endTag("", "DN103");

				xml.startTag("", "DN104");
				xml.text("" + cap100.getDN104());
				xml.endTag("", "DN104");

				xml.startTag("", "DN105");
				xml.text("" + cap100.getDN105());
				xml.endTag("", "DN105");

				xml.startTag("", "DN106");
				xml.text("" + cap100.getDN106());
				xml.endTag("", "DN106");

				xml.startTag("", "DN107");
				xml.text("" + cap100.getDN107());
				xml.endTag("", "DN107");

				xml.startTag("", "DN108");
				xml.text("" + cap100.getDN108());
				xml.endTag("", "DN108");

				xml.startTag("", "DN109");
				xml.text("" + cap100.getDN109());
				xml.endTag("", "DN109");

				xml.startTag("", "DN110");
				xml.text("" + cap100.getDN110());
				xml.endTag("", "DN110");

				xml.startTag("", "DN111");
				xml.text("" + cap100.getDN111());
				xml.endTag("", "DN111");

				xml.startTag("", "DN112");
				xml.text("" + cap100.getDN112());
				xml.endTag("", "DN112");

				xml.startTag("", "DN113");
				xml.text("" + cap100.getDN113());
				xml.endTag("", "DN113");

				xml.startTag("", "DN114");
				xml.text("" + cap100.getDN114());
				xml.endTag("", "DN114");

				xml.startTag("", "DN115");
				xml.text("" + cap100.getDN115());
				xml.endTag("", "DN115");

				xml.startTag("", "DN116");
				xml.text("" + cap100.getDN116());
				xml.endTag("", "DN116");

				xml.startTag("", "DN117");
				xml.text("" + cap100.getDN117());
				xml.endTag("", "DN117");

				xml.startTag("", "DN118");
				xml.text("" + cap100.getDN118());
				xml.endTag("", "DN118");

				xml.startTag("", "DN119");
				xml.text("" + cap100.getDN119());
				xml.endTag("", "DN119");

				xml.startTag("", "DN120");
				xml.text("" + cap100.getDN120());
				xml.endTag("", "DN120");

				xml.startTag("", "DN121");
				xml.text("" + cap100.getDN121());
				xml.endTag("", "DN121");

				xml.startTag("", "FEC_INI");
				xml.text(Texto(cap100.getFEC_INI()));
				xml.endTag("", "FEC_INI");

				xml.startTag("", "HOR_INI");
				xml.text(Texto(cap100.getHOR_INI()));
				xml.endTag("", "HOR_INI");

				xml.startTag("", "FEC_FIN");
				xml.text(Texto(cap100.getFEC_FIN()));
				xml.endTag("", "FEC_FIN");

				xml.startTag("", "HOR_FIN");
				xml.text(Texto(cap100.getHOR_FIN()));
				xml.endTag("", "HOR_FIN");

				xml.startTag("", "OBS_1");
				xml.text(Texto(cap100.getOBS_2()));
				xml.endTag("", "OBS_1");

				
				xml.startTag("", "USR_CREACION");
				xml.text(Texto(cap100.getUSR_CREACION()));
				xml.endTag("", "USR_CREACION");
				
				xml.startTag("", "FEC_CREACION");
				xml.text(Texto(cap100.getFEC_CREACION()));
				xml.endTag("", "FEC_CREACION");
				
				xml.startTag("", "USR_EDICION");
				xml.text(Texto(cap100.getUSR_EDICION()));
				xml.endTag("", "USR_EDICION");
				
				xml.startTag("", "FEC_EDICION");
				xml.text(Texto(cap100.getFEC_EDICION()));
				xml.endTag("", "FEC_EDICION");
				
				
				xml.endTag("", "CAP100");
			}

			// CAP200
			if (lCap200 != null) {
				for (Cap200 cap200 : lCap200) {
					xml.startTag("", "CAP200");

					xml.startTag("", "ID_N");
					xml.text(Texto(cap200.getID_N()));
					xml.endTag("", "ID_N");
					
					xml.startTag("", "ANIO");
					xml.text(Texto(cap200.getANIO()));
					xml.endTag("", "ANIO");

					xml.startTag("", "NRO_MREG");
					xml.text("" + cap200.getNRO_MREG());
					xml.endTag("", "NRO_MREG");

					xml.startTag("", "IH201");
					xml.text("" + cap200.getIH201());
					xml.endTag("", "IH201");

					xml.startTag("", "IH201_O");
					xml.text(Texto(cap200.getIH201_O()));
					xml.endTag("", "IH201_O");

					xml.startTag("", "IH202");
					xml.text("" + cap200.getIH202());
					xml.endTag("", "IH202");

					xml.startTag("", "IH202_O");
					xml.text(Texto(cap200.getIH202_O()));
					xml.endTag("", "IH202_O");

					xml.startTag("", "IH203");
					xml.text("" + cap200.getIH203());
					xml.endTag("", "IH203");

					xml.startTag("", "IH203_O");
					xml.text(Texto(cap200.getIH203_O()));
					xml.endTag("", "IH203_O");

					xml.startTag("", "IH_NRO_DOC");
					xml.text(Texto(cap200.getIH_NRO_DOC()));
					xml.endTag("", "IH_NRO_DOC");

					xml.startTag("", "IH204");
					xml.text("" + cap200.getIH204());
					xml.endTag("", "IH204");

					xml.startTag("", "IH204_O");
					xml.text(Texto(cap200.getIH204_O()));
					xml.endTag("", "IH204_O");

					xml.startTag("", "IH205_DIA");
					xml.text(Texto(cap200.getIH205_DIA()));
					xml.endTag("", "IH205_DIA");

					xml.startTag("", "IH205_MES");
					xml.text(Texto(cap200.getIH205_MES()));
					xml.endTag("", "IH205_MES");

					xml.startTag("", "IH206_HOR");
					xml.text(Texto(cap200.getIH206_HOR()));
					xml.endTag("", "IH206_HOR");

					xml.startTag("", "IH206_MIN");
					xml.text(Texto(cap200.getIH206_MIN()));
					xml.endTag("", "IH206_MIN");

					xml.startTag("", "IH207");
					xml.text("" + cap200.getIH207());
					xml.endTag("", "IH207");

					xml.startTag("", "IH207_O");
					xml.text(Texto(cap200.getIH207_O()));
					xml.endTag("", "IH207_O");

					xml.startTag("", "IH208");
					xml.text("" + cap200.getIH208());
					xml.endTag("", "IH208");

					xml.startTag("", "IH208_O");
					xml.text(Texto(cap200.getIH208_O()));
					xml.endTag("", "IH208_O");

					xml.startTag("", "IH209_1");
					xml.text("" + cap200.getIH209_1());
					xml.endTag("", "IH209_1");

					xml.startTag("", "IH209_2");
					xml.text("" + cap200.getIH209_2());
					xml.endTag("", "IH209_2");

					xml.startTag("", "IH209_3");
					xml.text("" + cap200.getIH209_3());
					xml.endTag("", "IH209_3");

					xml.startTag("", "IH209_4");
					xml.text("" + cap200.getIH209_4());
					xml.endTag("", "IH209_4");

					xml.startTag("", "IH209_5");
					xml.text("" + cap200.getIH209_5());
					xml.endTag("", "IH209_5");

					xml.startTag("", "IH209_6");
					xml.text("" + cap200.getIH209_6());
					xml.endTag("", "IH209_6");

					xml.startTag("", "IH209_7");
					xml.text("" + cap200.getIH209_7());
					xml.endTag("", "IH209_7");

					xml.startTag("", "IH209_8");
					xml.text("" + cap200.getIH209_8());
					xml.endTag("", "IH209_8");

					xml.startTag("", "IH209_9");
					xml.text("" + cap200.getIH209_9());
					xml.endTag("", "IH209_9");

					xml.startTag("", "IH209_10");
					xml.text("" + cap200.getIH209_10());
					xml.endTag("", "IH209_10");

					xml.startTag("", "IH209_11");
					xml.text("" + cap200.getIH209_11());
					xml.endTag("", "IH209_11");

					xml.startTag("", "IH209_12");
					xml.text("" + cap200.getIH209_12());
					xml.endTag("", "IH209_12");

					xml.startTag("", "IH209_13");
					xml.text("" + cap200.getIH209_13());
					xml.endTag("", "IH209_13");

					xml.startTag("", "IH209_14");
					xml.text("" + cap200.getIH209_14());
					xml.endTag("", "IH209_14");

					xml.startTag("", "IH209_15");
					xml.text("" + cap200.getIH209_15());
					xml.endTag("", "IH209_15");

					xml.startTag("", "IH209_16");
					xml.text("" + cap200.getIH209_16());
					xml.endTag("", "IH209_16");

					xml.startTag("", "IH209_16_O");
					xml.text(Texto(cap200.getIH209_16_O()));
					xml.endTag("", "IH209_16_O");

					xml.startTag("", "IH210");
					xml.text("" + cap200.getIH210());
					xml.endTag("", "IH210");

					xml.startTag("", "IH211");
					xml.text("" + cap200.getIH211());
					xml.endTag("", "IH211");

					xml.startTag("", "IH211_O");
					xml.text(Texto(cap200.getIH211_O()));
					xml.endTag("", "IH211_O");

					xml.startTag("", "IH212");
					xml.text("" + cap200.getIH212());
					xml.endTag("", "IH212");

					xml.startTag("", "IH213");
					xml.text("" + cap200.getIH213());
					xml.endTag("", "IH213");

					xml.startTag("", "OBS_2");
					xml.text(Texto(cap200.getOBS_2()));
					xml.endTag("", "OBS_2");

					
					xml.startTag("", "USR_CREACION");
					xml.text(Texto(cap200.getUSR_CREACION()));
					xml.endTag("", "USR_CREACION");
					
					xml.startTag("", "FEC_CREACION");
					xml.text(Texto(cap200.getFEC_CREACION()));
					xml.endTag("", "FEC_CREACION");
					
					xml.startTag("", "USR_EDICION");
					xml.text(Texto(cap200.getUSR_EDICION()));
					xml.endTag("", "USR_EDICION");
					
					xml.startTag("", "FEC_EDICION");
					xml.text(Texto(cap200.getFEC_EDICION()));
					xml.endTag("", "FEC_EDICION");
					
					
					xml.endTag("", "CAP200");
				}
			}

			// CAP300
			if (lCap300 != null) {
				for (Cap300 cap300 : lCap300) {
					xml.startTag("", "CAP300");

					xml.startTag("", "ID_N");
					xml.text(Texto(cap300.getID_N()));
					xml.endTag("", "ID_N");

					xml.startTag("", "ANIO");
					xml.text(Texto(cap300.getANIO()));
					xml.endTag("", "ANIO");

					xml.startTag("", "NRO_MREG");
					xml.text("" + cap300.getNRO_MREG());
					xml.endTag("", "NRO_MREG");

					xml.startTag("", "NRO_VFREG");
					xml.text("" + cap300.getNRO_VFREG());
					xml.endTag("", "NRO_VFREG");

					xml.startTag("", "IVH301A");
					xml.text(Texto(cap300.getIVH301A()));
					xml.endTag("", "IVH301A");

					xml.startTag("", "IVH301B");
					xml.text(Texto(cap300.getIVH301B()));
					xml.endTag("", "IVH301B");

					xml.startTag("", "IVH301C");
					xml.text(Texto(cap300.getIVH301C()));
					xml.endTag("", "IVH301C");

					xml.startTag("", "IVH302");
					xml.text("" + cap300.getIVH302());
					xml.endTag("", "IVH302");

					xml.startTag("", "IVH302_N");
					xml.text(Texto(cap300.getIVH302_N()));
					xml.endTag("", "IVH302_N");

					xml.startTag("", "IVH303");
					xml.text("" + cap300.getIVH303());
					xml.endTag("", "IVH303");

					xml.startTag("", "IVH304_DIA");
					xml.text(Texto(cap300.getIVH304_DIA()));
					xml.endTag("", "IVH304_DIA");

					xml.startTag("", "IVH304_MES");
					xml.text(Texto(cap300.getIVH304_MES()));
					xml.endTag("", "IVH304_MES");

					xml.startTag("", "IVH304_ANIO");
					xml.text(Texto(cap300.getIVH304_ANIO()));
					xml.endTag("", "IVH304_ANIO");

					xml.startTag("", "IVH305");
					xml.text("" + cap300.getIVH305());
					xml.endTag("", "IVH305");

					xml.startTag("", "IVH306");
					xml.text("" + cap300.getIVH306());
					xml.endTag("", "IVH306");

					xml.startTag("", "IVH306_O");
					xml.text(Texto(cap300.getIVH306_O()));
					xml.endTag("", "IVH306_O");

					xml.startTag("", "IVH307");
					xml.text("" + cap300.getIVH307());
					xml.endTag("", "IVH307");

					xml.startTag("", "IVH307_O");
					xml.text(Texto(cap300.getIVH307_O()));
					xml.endTag("", "IVH307_O");

					xml.startTag("", "IVH308");
					xml.text("" + cap300.getIVH308());
					xml.endTag("", "IVH308");

					xml.startTag("", "IVH309");
					xml.text("" + cap300.getIVH309());
					xml.endTag("", "IVH309");

					xml.startTag("", "IVH309_O");
					xml.text(Texto(cap300.getIVH309_O()));
					xml.endTag("", "IVH309_O");

					xml.startTag("", "OBS_3");
					xml.text(Texto(cap300.getOBS_3()));
					xml.endTag("", "OBS_3");

					xml.startTag("", "FEC_INI");
					xml.text(Texto(cap300.getFEC_INI()));
					xml.endTag("", "FEC_INI");

					xml.startTag("", "HOR_INI");
					xml.text(Texto(cap300.getHOR_INI()));
					xml.endTag("", "HOR_INI");

					xml.startTag("", "FEC_FIN");
					xml.text(Texto(cap300.getFEC_FIN()));
					xml.endTag("", "FEC_FIN");

					xml.startTag("", "HOR_FIN");
					xml.text(Texto(cap300.getHOR_FIN()));
					xml.endTag("", "HOR_FIN");


					xml.startTag("", "USR_CREACION");
					xml.text(Texto(cap300.getUSR_CREACION()));
					xml.endTag("", "USR_CREACION");
					
					xml.startTag("", "FEC_CREACION");
					xml.text(Texto(cap300.getFEC_CREACION()));
					xml.endTag("", "FEC_CREACION");
					
					xml.startTag("", "USR_EDICION");
					xml.text(Texto(cap300.getUSR_EDICION()));
					xml.endTag("", "USR_EDICION");
					
					xml.startTag("", "FEC_EDICION");
					xml.text(Texto(cap300.getFEC_EDICION()));
					xml.endTag("", "FEC_EDICION");
					
					
					xml.endTag("", "CAP300");
				}
			}

			// CAP400
			if (lCap400 != null) {
				for (Cap400 cap400 : lCap400) {
					xml.startTag("", "CAP400");

					xml.startTag("", "ID_N");
					xml.text(Texto(cap400.getID_N()));
					xml.endTag("", "ID_N");

					xml.startTag("", "ANIO");
					xml.text(Texto(cap400.getANIO()));
					xml.endTag("", "ANIO");

					xml.startTag("", "NRO_MREG");
					xml.text("" + cap400.getNRO_MREG());
					xml.endTag("", "NRO_MREG");

					xml.startTag("", "NRO_PVREG");
					xml.text("" + cap400.getNRO_PVREG());
					xml.endTag("", "NRO_PVREG");

					xml.startTag("", "IVM401A");
					xml.text(Texto(cap400.getIVM401A()));
					xml.endTag("", "IVM401A");

					xml.startTag("", "IVM401B");
					xml.text(Texto(cap400.getIVM401B()));
					xml.endTag("", "IVM401B");

					xml.startTag("", "IVM401C");
					xml.text(Texto(cap400.getIVM401C()));
					xml.endTag("", "IVM401C");

					xml.startTag("", "IVM402");
					xml.text("" + cap400.getIVM402());
					xml.endTag("", "IVM402");

					xml.startTag("", "IVH402_N");
					xml.text(Texto(cap400.getIVH402_N()));
					xml.endTag("", "IVH402_N");

					xml.startTag("", "IVM403");
					xml.text("" + cap400.getIVM403());
					xml.endTag("", "IVM403");

					xml.startTag("", "IVM404_DIA");
					xml.text(Texto(cap400.getIVM404_DIA()));
					xml.endTag("", "IVM404_DIA");

					xml.startTag("", "IVM404_MES");
					xml.text(Texto(cap400.getIVM404_MES()));
					xml.endTag("", "IVM404_MES");

					xml.startTag("", "IVM404_ANIO");
					xml.text(Texto(cap400.getIVM404_ANIO()));
					xml.endTag("", "IVM404_ANIO");

					xml.startTag("", "IVM405");
					xml.text("" + cap400.getIVM405());
					xml.endTag("", "IVM405");

					xml.startTag("", "IVM406");
					xml.text("" + cap400.getIVM406());
					xml.endTag("", "IVM406");

					xml.startTag("", "IVM406_O");
					xml.text("" + cap400.getIVM406_O());
					xml.endTag("", "IVM406_O");

					xml.startTag("", "IVM407");
					xml.text("" + cap400.getIVM407());
					xml.endTag("", "IVM407");

					xml.startTag("", "IVM407_O");
					xml.text(Texto(cap400.getIVM407_O()));
					xml.endTag("", "IVM407_O");

					xml.startTag("", "IVM408");
					xml.text("" + cap400.getIVM408());
					xml.endTag("", "IVM408");

					xml.startTag("", "OBS_4");
					xml.text(Texto(cap400.getOBS_4()));
					xml.endTag("", "OBS_4");

					xml.startTag("", "FEC_INI");
					xml.text(Texto(cap400.getFEC_INI()));
					xml.endTag("", "FEC_INI");

					xml.startTag("", "HOR_INI");
					xml.text(Texto(cap400.getHOR_INI()));
					xml.endTag("", "HOR_INI");

					xml.startTag("", "FEC_FIN");
					xml.text(Texto(cap400.getFEC_FIN()));
					xml.endTag("", "FEC_FIN");

					xml.startTag("", "HOR_FIN");
					xml.text(Texto(cap400.getHOR_FIN()));
					xml.endTag("", "HOR_FIN");


					xml.startTag("", "USR_CREACION");
					xml.text(Texto(cap400.getUSR_CREACION()));
					xml.endTag("", "USR_CREACION");
					
					xml.startTag("", "FEC_CREACION");
					xml.text(Texto(cap400.getFEC_CREACION()));
					xml.endTag("", "FEC_CREACION");
					
					xml.startTag("", "USR_EDICION");
					xml.text(Texto(cap400.getUSR_EDICION()));
					xml.endTag("", "USR_EDICION");
					
					xml.startTag("", "FEC_EDICION");
					xml.text(Texto(cap400.getFEC_EDICION()));
					xml.endTag("", "FEC_EDICION");
					
					
					xml.endTag("", "CAP400");
				}
			}

			xml.endTag("", "script");
			xml.endDocument();
			fos.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	private String Texto(String texto) {
		if (texto == null) {
			return "";
		} else {
			return texto;
		}
	}
	
	private String Texto(int nro) {
		return nro+"";
	}
}
